﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Resources;
using System.Reflection;

namespace MultiLanguageTest.Language
{
    /// <summary>
    /// 语言辅助类
    /// </summary>
    public class LanguageHelper
    {
        private static Dictionary<string, ResourceManager> languageDic = new Dictionary<string, ResourceManager>();

        private const string DefaultCulture = "CN";

        private static ResourceManager GetResourceManager(string culture)
        {
            if (languageDic.ContainsKey(culture))
            {
                return languageDic[culture];
            }

            var resxFile = $"MultiLanguageTest.Language.LanguageRes.Res{culture}";

            //var resMgr = new ResourceManager(resxFile, typeof(LanguageHelper).Assembly);
            var resMgr = new ResourceManager(resxFile, Assembly.GetExecutingAssembly());

            languageDic.Add(culture, resMgr);

            return resMgr;
        }

        public static string GetText(string name, string culture = "CN")
        {
            var resMgr = GetResourceManager(culture);

            var val = resMgr.GetString(name);

            //使用默认值
            if (val != null)
            {
                return val;
            }
            else
            {
                return GetResourceManager(DefaultCulture).GetString(name);
            }
        }

        public static string GetTextInCurCulture(string name)
        {
            var culture = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
            return GetText(name, culture);
        }
    }
}
